package engine

import (
	"fmt"
	"strconv"
	"webProject/crawler"
	"webProject/crawler/lagou/fetcher"
	"webProject/crawler/lagou/parser"
	"webProject/crawler/scheduler"
)

type Engine interface {
	Run()
	createWorker(in chan string,out chan string,scheduler scheduler.Scheduler)
}
type CurrentEngine struct {
	//调度器
	Scheduler scheduler.Scheduler
	//工人数量
	WorkerCount int
	Processor Processor
}

type Processor func(request crawler.Request) (crawler.ParseResult, error)



func (c *CurrentEngine) createWorker(in chan crawler.Request, out chan crawler.ParseResult, scheduler scheduler.Scheduler) {
	//三个参数的含义
	//in chan string 用于 scheduler传输数据给worker进行处理
	//out chan string 用于worker处理完数据后将数据返回给Engine
	//scheduler Scheduler 当worker处理完成之后，
	//调用scheduler.WorkerReady将自己放进scheduler的WorkerChan中去
	go func() {
		for{
			scheduler.WorkerReady(in)
			//从in中获取请求数据
			request := <-in
			fmt.Printf("接收到请求：%s %s %v \n",request.KD,request.PN,request.Parser)
			//调用Engine里的处理函数进行处理
			result, e := c.Processor(request)
			if e != nil{
				//不进行处理，进行下一个请求处理
			}
			//将结果传输出去
			out <- result
		}
	}()

}

func (c *CurrentEngine) Run() {
	//启动Scheduler
	c.Scheduler.Run()
	go fetcher.GetSID()
	//out用于接收处理完的结果
	out := make(chan crawler.ParseResult)

	//启动多个Worker
	for i:=0;i<c.WorkerCount;i++{
		c.createWorker(c.Scheduler.WorkerChan(),out,c.Scheduler)
	}

	//提交任务
	for i:=1;i<100;i++{
		c.Scheduler.Submit(crawler.Request{
			PN:     strconv.Itoa(i),
			KD:     "北京",
			Parser: parser.ParseJobFromJson,
		})
	}
	i:=0
	for{
		jobItems:= <- out
		for _,item := range jobItems.Items{
			fmt.Printf("【%d】%v \n",i,item)
			i++
		}
	}
}
